//
//  UWScoreBoard.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-12-12.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "UWScoreBoard.h"

@implementation UWScoreBoard

@synthesize historyView;
@synthesize opponentScore;
@synthesize score;
@synthesize userName;
@synthesize opponentName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        isDragging = NO;
        oldSpeed = 0;
        curtainIsOpen = NO;
        [self.view setClipsToBounds:NO];
        [self.view setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)curtainTouchBegan:(NSSet *)touches
{
    [self touchesBegan:touches withEvent:nil];
}

- (void)curtainTouchEnded:(NSSet *)touches
{
    [self touchesEnded:touches withEvent:nil];
}

- (void)curtainTouchCancelled:(NSSet *)touches
{
    [self touchesCancelled:touches withEvent:nil];
}

- (void)curtainTouchMoved:(NSSet *)touches
{
    [self touchesMoved:touches withEvent:nil];
}

- (void)setHistoryView:(UIView *)input
{
    historyView = [input retain];
    
    [historyView setClipsToBounds:NO];
    
    historyView.frame = CGRectMake(0, -320, 320, 346);
    
    self.view.frame = CGRectMake(0, 25, 320, 24);
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - Touch Events

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    previousTimestamp = event.timestamp;
    isDragging = YES;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    isDragging = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (isDragging) {
        UITouch *touch = [touches anyObject];
        CGPoint pos = [touch locationInView:self.view.superview];
        CGPoint oldPos = [touch previousLocationInView:self.view.superview];
        
        int y = MIN(MAX(pos.y, 25), 345);
        
        historyView.frame = CGRectMake(0, -345 + y, 320, 346);
        self.view.frame = CGRectMake(0, y, 320, 24);
        
        float distanceFromPrevious = pos.y - oldPos.y;
        float timeSincePrevious = event.timestamp - previousTimestamp;
        
        float lambda = 0.9;
        float newSpeed = (1.0 - lambda) * oldSpeed + lambda * (distanceFromPrevious / timeSincePrevious);
        
        y = 25;
        
        if (pos.y > oldPos.y) {
            y = 346;
            curtainIsOpen = YES;
        } else {
            curtainIsOpen = NO;
        }
        
        float distance = y - self.view.frame.origin.y;
    
        float duration = abs(distance) / (float)MAX(MIN(abs(newSpeed), 1000), 0)+0.00001;
            
        duration = MIN(0.5, duration);
        //0.2*(1 - (, 0) / 1000.f)) + 0.05;
        
        oldSpeed = 0;
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:duration];
        
        historyView.frame = CGRectMake(0, -346 + y, 320, 346);
        self.view.frame = CGRectMake(0, y, 320, 24);
        
        [UIView commitAnimations];
    }
    
    isDragging = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (isDragging) {
        UITouch *touch = [touches anyObject];
        CGPoint pos = [touch locationInView:self.view.superview];
        CGPoint oldPos = [touch locationInView:self.view.superview];
        
        int y = MIN(MAX(pos.y, 25), 345);
        
        historyView.frame = CGRectMake(0, -345 + y, 320, 346);
        self.view.frame = CGRectMake(0, y, 320, 24);
        
        float distanceFromPrevious = pos.y - oldPos.y;
        float timeSincePrevious = event.timestamp - previousTimestamp;
        
        float lambda = 0.9;
        float newSpeed = (1.0 - lambda) * oldSpeed + lambda * (distanceFromPrevious / timeSincePrevious);
        oldSpeed = newSpeed;
        
        previousTimestamp = event.timestamp;
    }
}

- (BOOL)isOpen
{
    return curtainIsOpen;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setOpponentScore:nil];
    [self setScore:nil];
    [self setUserName:nil];
    [self setOpponentName:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [opponentScore release];
    [score release];
    [userName release];
    [opponentName release];
    [super dealloc];
}
@end
